package com.weme.common.util



import android.util.Log
import com.luck.picture.lib.utils.ToastUtils.showToast
import com.weme.common.AppConfig
import com.weme.common.BaseApplication
import io.agora.chat.BuildConfig
import io.agora.rtc2.*

interface RtcEngineListener {
    fun onUserJoined(uid: Int)
    fun onFirstRemoteVideoFrame()
}

object RtcEngineSingleton {
    private var mRtcEngine: RtcEngine? = null
    private var mListener: RtcEngineListener? = null


    private fun initRtcEngine() {
        Log.e("rtc", "----------------------")
        val config = RtcEngineConfig()
        config.mContext = BaseApplication.sInstance
        config.mAppId = AppConfig.APP_ID
        config.mEventHandler = mRtcEventHandler
        config.mAudioScenario =
            Constants.AudioScenario.getValue(Constants.AudioScenario.CHATROOM)
        mRtcEngine = RtcEngine.create(config)
        mRtcEngine?.setAudioProfile(Constants.AUDIO_PROFILE_SPEECH_STANDARD)
    }

    fun getRtcEngine(): RtcEngine {
        if (mRtcEngine == null) {
            synchronized(this) {
                if (mRtcEngine == null) {
                    initRtcEngine()
                }
            }
        }
        return mRtcEngine!!
    }

    fun setRtcEngineListener(listener: RtcEngineListener) {
        mListener = listener

    }

    private val mRtcEventHandler: IRtcEngineEventHandler
        get() = object : IRtcEngineEventHandler() {
            // 监听频道内的远端用户，获取用户的 uid 信息
            override fun onUserJoined(uid: Int, elapsed: Int) {
                mListener?.onUserJoined(uid)
//                Log.e("LiveRoom", "onUserJoined = " + uid)
//                runOnUiThread { // 获取 uid 后，设置远端视频视图
//                    setupRemoteVideo(uid)
//                }
            }

            override fun onFirstRemoteVideoFrame(uid: Int, width: Int, height: Int, elapsed: Int) {
                super.onFirstRemoteVideoFrame(uid, width, height, elapsed) //渲染器已接收首帧远端视频回调。
                mListener?.onFirstRemoteVideoFrame()
                //收到远端首针回调 掉一下接口 告诉服务端
//                getViewModel().setFirstFrame()
//                getViewModel().startTimer()


            }

            override fun onError(err: Int) {
                super.onError(err)
                Log.e("LiveRoom", "onError = " + err)

            }

            //加入频道成功
            override fun onJoinChannelSuccess(channel: String?, uid: Int, elapsed: Int) {
                super.onJoinChannelSuccess(channel, uid, elapsed)
                Log.e("LiveRoom", "onJoinChannelSuccess = " + channel)
            }

            override fun onUserInfoUpdated(uid: Int, userInfo: UserInfo?) {
                super.onUserInfoUpdated(uid, userInfo)
                Log.e("LiveRoom", "onUserInfoUpdated = " + uid) //远端用户信息已更新回调。
            }

            override fun onConnectionStateChanged(state: Int, reason: Int) {
                super.onConnectionStateChanged(state, reason)


                Log.e("LiveRoom", "onConnectoionStateChanged->, state->$state, reason->$reason")
            }

        }

    fun releaseRtcEngine() {
//        mRtcEngine?.destroy()
//        mRtcEngine = null
    }
}

